package com.rdc.jiajiadictionary.translate;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.rdc.jiajiadictionary.R;
import com.rdc.jiajiadictionary.adapter.HistoryAdapter;
import com.rdc.jiajiadictionary.bean.Word;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HistoryFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HistoryFragment extends Fragment implements HistoryViewInterface{

    private HistoryPresenter historyPresenter;
    private RecyclerView recyclerView;
    private TextView cleanTv;
    private Context mContext;
    private ReplaceFragmentCallBack callBack;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    public HistoryFragment(){

    }
    public HistoryFragment(ReplaceFragmentCallBack replaceListener){
       this.callBack = replaceListener;// Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HistoryFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HistoryFragment newInstance(String param1, String param2) {
        HistoryFragment fragment = new HistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        historyPresenter.start();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.history_fragment, container, false);
        recyclerView = view.findViewById(R.id.recycle_history);
        cleanTv = (TextView) view.findViewById(R.id.tv_clean);
        new HistoryPresenter(this,mContext);

        //删除历史记录时的提醒对话框
        cleanTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                AlertDialog alertDialog;
                builder.setTitle("提示");
                builder.setMessage("确定要删除吗?");

                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                builder.setNeutralButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (historyPresenter != null) {
                            historyPresenter.deleteHistory();
                            historyPresenter.getWords();
                        }

                    }
                });
                builder.show();
            }
        });

        return view;
    }

    /**
     * 绑定presenter
     * @param presenter
     */
    @Override
    public void setPresenter(HistoryPresenter presenter) {
        this.historyPresenter = presenter;
    }

    /**
     * 提供历史搜索RecycleView子项适配器适配方法给presenter调用，实现子项点击的回调接口
     * @param words
     */
    @Override
    public void adaptRecycleView(List<Word> words){
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        StaggeredGridLayoutManager layoutManager = new
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        HistoryAdapter historyAdapter = new HistoryAdapter(words);
        historyAdapter.setOnItemClickListener(new HistoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Word word = words.get(position);
                if (callBack != null){
                    callBack.onClickReplace(word.getWord_name());
                }
            }
        });
        recyclerView.setAdapter(historyAdapter);
    }

    /**
     * 设置子项点击后的更换碎片回调接口
     */
    public interface ReplaceFragmentCallBack{
        void onClickReplace(String text);
    }
}